<?php
/**
 * @file
 * @package mileage
 * @version $Id$
**/

if (!defined('XOOPS_ROOT_PATH')) {
	exit;
}

require_once MILEAGE_TRUST_PATH . '/class/AbstractListAction.class.php';

/**
 * Mileage_RecListAction
**/
class Mileage_Admin_RecListAction extends Mileage_AbstractListAction
{
	private $_loginMile;
	private $_totalPoint = 0;

	/**
	 * &_getHandler
	 * 
	 * @param   void
	 * 
	 * @return  Mileage_MileHandler
	**/
	protected function &_getHandler()
	{
		$handler =& $this->mAsset->getObject('handler', 'rec');
		return $handler;
	}

	/**
	 * &_getFilterForm
	 * 
	 * @param   void
	 * 
	 * @return  Mileage_RecFilterForm
	**/
	protected function &_getFilterForm()
	{
		// $filter =& new Mileage_RecFilterForm();
		$filter =& $this->mAsset->getObject('filter', 'rec', TRUE);// 3rd arg is FALSE(not admin) 
		$filter->prepare($this->_getPageNavi(), $this->_getHandler());
		return $filter;
	}

	/**
	 * _getBaseUrl
	 * 
	 * @param   void
	 * 
	 * @return  string
	**/
	protected function _getBaseUrl()
	{
		return './index.php?action=RecList';
	}

	/**
	 * getDefaultView
	 * 
	 * @param   void
	 * 
	 * @return  Enum
	**/
	public function getDefaultView()
	{
		$this->mFilter =& $this->_getFilterForm();
		$this->mFilter->fetch();
		$handler =& $this->_getHandler();
		$this->_mObjects =& $handler->getObjects($this->mFilter->getCriteria());

		$tempMile = array();
		for ($i = 0; $i<count($this->_mObjects); $i++) {
			$mile_id = $this->_mObjects[$i]->get('mile_id');
			if (isset($tempMile[$mile_id])) {
				$this->_mObjects[$i]->mileName = $tempMile[$mile_id];
			} else {
				$mile = $this->_mObjects[$i]->getMileObject($this->mAsset->mDirname);
				if (is_object($mile)) {
					$tempMile[$mile_id] = $mile->get('name');
					$this->_mObjects[$i]->mileName = $tempMile[$mile_id];
				}
			}

		}

		// Total Point case UID
		if (isset($this->mFilter->uid) && $this->mFilter->uid > 0) {
			$this->_totalPoint = Mileage_Utils::totalMileByUid($this->mAsset->mDirname, $this->mFilter->uid);
		}

		return MILEAGE_FRAME_VIEW_INDEX;
	}

	/**
	 * executeViewIndex
	 * 
	 * @param   XCube_RenderTarget  &$render
	 * 
	 * @return  void
	**/
	public function executeViewIndex(&$render)
	{
		$render->setTemplateName('rec_list.html');
		$render->setAttribute('dirname', $this->mAsset->mDirname);
		$render->setAttribute('objects', $this->_mObjects);
		$render->setAttribute('pageNavi', $this->mFilter->mNavi);
		$render->setAttribute('totalPoint', $this->_totalPoint);
	}

}
